package benchmark.myorm;

import org.junit.After;
import org.mb.criteria.Criteria;
import org.mb.datasource.HsqldbDataSourceFactory;
import org.mb.mapper.DataMapper;
import org.mb.session.SessionFactory;

import static org.junit.Assert.*;

public class OrmBenchmark {

	static {
		SessionFactory.configDataSource(new HsqldbDataSourceFactory().getDataSource());
	}

	public void before() {
		SessionFactory.getSession().open();

		DataMapper.executeDDLIgnoringErrors("DROP TABLE DOG");
		DataMapper.executeDDLIgnoringErrors("DROP SEQUENCE DOG_SEQUENCE");

		DataMapper.executeDDL("CREATE SEQUENCE DOG_SEQUENCE AS INTEGER START WITH 1 INCREMENT BY 1");
		DataMapper.executeDDL(
		/**/"CREATE TABLE DOG (" +
		/**/"ID_DOG INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 100) PRIMARY KEY," +
		/**/"NAME VARCHAR(20)," +
		/**/"AGE INTEGER)"
		/**/);
		SessionFactory.getSession().commit();
	}

	public void testname() throws Exception {

		SessionFactory.getSession().open();

		try {
			OrmDog d = new OrmDog(null, "din", 9);
			d.store();

			d = Criteria.selectById(OrmDog.class, d.getId());

			assertEquals("[ID_DOG=*, NAME=din, AGE=9]", d.toString().replaceAll("ID_DOG=\\d+", "ID_DOG=*"));

			SessionFactory.getSession().commit();
		} catch (final Exception e) {
			SessionFactory.getSession().rollback();
			throw e;
		}
	}

	@After
	public void after() {
		SessionFactory.getSession().open();
		DataMapper.executeDDL("DROP SEQUENCE DOG_SEQUENCE");
		DataMapper.executeDDL("DROP TABLE DOG");
		SessionFactory.getSession().commit();
	}

}
